﻿using AutoMapper;
using MediatR;
using SupplyChain.Domain.Entities;
using SupplyChain.Infrastructure.Interface;
using SupplyChain.Write.API.Application.Command.Supplier;

namespace SupplyChain.Write.API.Application.CommandHandler.Supplier
{
    public class DelSupplierProfileHandler : IRequestHandler<DelSupplierProfileCommand, bool>
    {
        private readonly IBaseRepository<SupplierProfile> repository;
        private readonly IMapper mapper;

        public DelSupplierProfileHandler(IBaseRepository<SupplierProfile> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(DelSupplierProfileCommand request, CancellationToken cancellationToken)
        {
            if (request.Id == 0) return await Task.FromResult(false);
            
            SupplierProfile supplier = await repository.GetFirstOrDefaultAsync(x => !x.IsDel && x.Id == request.Id);
            supplier.IsDel = !supplier.IsDel;
            return await Task.FromResult(await repository.UpdateAsync(supplier));
        }
    }
}
